#ifndef TCPSESSION_H
#define TCPSESSION_H

#include <QObject>
#include <QTcpSocket>
#include <QVector>
#include <QStringList>
#include <QImage>
#include <QMutex>
#include "pack.h"

class TcpSession : public QObject
{
    Q_OBJECT
public:
    static TcpSession* getInstance(const QString & hostname, const int & port, QObject *parent=nullptr);
    explicit TcpSession(QTcpSocket* socket, QObject *parent=nullptr);
    virtual ~TcpSession();
    void clearSessionInfo();
    void readPacks();
    int getVectorSize(){return vector.size();};
    QTcpSocket* getQTcpSocket(){return socket;};
    Pack popFrontPack();
    //借助socket发送数据
    int send(char* buf, int buflen);
//    void disconnectFromHost();
    //保存一些客户端的信息
    void putValue(const QString & key, const QString & value);
    QString getValue(const QString & key);
signals:
    void readyRead();
    void disconnected();
    //
    void login_ret_signal(bool res, const QString & username="", const QString & nickname="");
    void reg_ret_signal(bool res, const QString & msg="");
    void house_add_signal(bool res, const QString & msg="");
    void house_enter_signal(bool res, const QString & msg="");
    void houselist_ret_signal(const QStringList& fields);
    void logout_call_signal(const QString & msg);
    void houseMems_signal(const QStringList& fields);
    void houseMsg_signal(const QString & msg="");
    void video_signal(QImage image);
    void camera_signal(QImage image);
    void gift_signal(const QString & nickname, const QString & houseid, const QString & gift);
    void bullet_signal(const QString & nickname, const QString & houseid, const QString & msg);

public slots:
    void onReadyRead();
    void onDisconnected();

private:
    QString hostname;
    int port;
    QTcpSocket* socket=nullptr;
    QVector<Pack> vector;
    int packsize=0;//当前报文的packsize，当此变量为0时，表示没有正在解报
    //
    QMap<QString, QString> sessionMap;
    //视频流缓存
    QByteArray videoByteArr;
    QByteArray cameraByteArr;
    //单例模式
    static TcpSession* instance;
    static QMutex mutex;
};

#endif // TCPSESSION_H
